package com.example.qianjun.ninetable;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    private int[][] setTable = new int[3][3];
    private EditText[] numbers = new EditText[9];

    private int[] nums = new int[9];
    private Button lookPreview;
    private Button lookForWays;
    private NineTable nineTable;

    private boolean checkBoardFlag = true;

    //输入为空标志位
    private boolean spaceFlag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CheckBoard.init();

        setContentView(R.layout.activity_setting);
        findViews();
        listenerinit();
    }

    public void findViews() {

        numbers[0] = (EditText) findViewById(R.id.number1);
        numbers[1] = (EditText) findViewById(R.id.number2);
        numbers[2] = (EditText) findViewById(R.id.number3);
        numbers[3] = (EditText) findViewById(R.id.number4);
        numbers[4] = (EditText) findViewById(R.id.number5);
        numbers[5] = (EditText) findViewById(R.id.number6);
        numbers[6] = (EditText) findViewById(R.id.number7);
        numbers[7] = (EditText) findViewById(R.id.number8);
        numbers[8] = (EditText) findViewById(R.id.number9);
        lookPreview = (Button) findViewById(R.id.lookPreview);
        lookForWays = (Button) findViewById(R.id.lookForWays);
        nineTable = (NineTable) findViewById(R.id.ninetablesetting);
    }

    public void listenerinit() {
        lookPreview.setOnClickListener(this);
        lookForWays.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lookPreview:
                setTablePreview();
                if (spaceFlag) {
                    spaceFlag = false;
                    dialogWindow();
                } else {
                    checkTable();
                    if (checkBoardFlag) {
                        CheckBoard.setTable(setTable);
                        nineTable.postInvalidate();
                    } else {
                        dialogWindow();

                    }
                }

                break;
            case R.id.lookForWays:
                setTablePreview();

                if (spaceFlag) {
                    spaceFlag = false;
                    dialogWindow();
                } else {
                    checkTable();
                    if (checkBoardFlag) {
                        CheckBoard.map.clear();
                        CheckBoard.setCountFlag(0);
                        CheckBoard.tableInit(CheckBoard.lookingTable);
                        CheckBoard.setTable(CheckBoard.compareTable, setTable);
                        Intent intent = new Intent(SettingActivity.this, ResultActivity.class);
                        startActivity(intent);
                    } else {
                        dialogWindow();

                    }
                }


                break;
        }
    }

    public void setTablePreview() {
        for (int i = 0; i < 9; i++) {

            if (numbers[i].getText().toString() == null || "".equals(numbers[i].getText().toString())) {
                spaceFlag = true;
                break;
            }


            int num = Integer.parseInt(numbers[i].getText().toString());

            if (i < 3) {
                setTable[i][0] = num;
            } else if (i < 6) {
                setTable[i - 3][1] = num;
            } else {
                setTable[i - 6][2] = num;
            }
            nums[i] = num;

        }
    }

    public void checkTable() {
        int count = 0;
        for (int i = 1; i < 10; i++) {
            for (int k = 0; k < 9; k++) {
                if (i == nums[k]) {
                    count++;
                }
            }
            if (count == 1) {
                checkBoardFlag = true;
                count = 0;
            } else {
                checkBoardFlag = false;
                count = 0;
                break;
            }
        }
        for (int i = 0; i < 9; i++) {
            Log.i("mike", nums[i] + "");
        }
    }

    public void dialogWindow() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("输入有误，请重新输入！");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create().show();
    }

}
